#! /usr/bin/python
import glob, sys, os, time

def convertTimeStr(timeStr):
  t = time.mktime(time.strptime(timeStr, '%Y-%m-%d %H:%M:%S'))
  return int(t * 1000)

def dumpMoteLabData(dataFile, dataDir):
  dfile = open(dataFile)
  dfile.readline()
  serialHeader = '0,255,255,0,0,9,0,114'
  upperByteMask = int('0xFF00',0)
  lowerByteMask = int('0x00FF',0)
  nodes = []
  fileDict = {}
  msgCounter = 0
  msg = range(12)
  print 'Dumping data'
  for aline in dfile:
    cols = aline.split('\t')
    nodeID = int(cols[18])
    if nodeID not in nodes:
      nodes.append(nodeID)
      fileDict[nodeID] = open(dataDir + '/node' + str(nodeID) + '.csv', 'w')
    tmp = cols[17]
    msg[10] = str(convertTimeStr(tmp))
    tmp = tmp.split(' ')
    msg[0] = tmp[1]                           # server time string
    msg[1] = cols[19][0:-1]                   # server seq
    msg[2] = serialHeader                     # useless header
    msg[3] = hex(int(cols[16]))               # msg type
    tmp = int(cols[4])                        # 6 bytes of msg body
    msg[4] = str(tmp >> 8)
    msg[5] = str(tmp & lowerByteMask)
    msg[6] = cols[5]
    msg[7] = cols[6]
    msg[8] = cols[7]
    msg[9] = cols[8]
    msg[11] = cols[15] + '\n'

    msgCounter += 1
    if msgCounter % 10000 == 0:
      sys.stdout.write('.')
      sys.stdout.flush()
    fileDict[nodeID].write(','.join(msg))

  for df in fileDict.values():
    df.close()
  dfile.close()
  print ''
  print len(nodes), 'nodes found,', msgCounter, 'messages written'

def usage():
  print 'convertMotelabData.py <data file> <exp #> <P/O>'
  print 'Note: P = Predictor Module enabled, O = Original CTP'

"""
Main method
"""
def main():

  if(len(sys.argv) != 4):
    usage()
    sys.exit(0)

  datfile = sys.argv[1]
  expNo = int(sys.argv[2])
  datadir = ''

  if sys.argv[3] in ['P','p']:
    datadir = os.path.dirname(sys.argv[1]) + '/motelab_exp' + str(expNo)
  elif sys.argv[3] in ['O','o']:
    datadir = os.path.dirname(sys.argv[1]) + '/motelab_original' + str(expNo)
  else:
    usage()
    sys.exit(0)

  if not os.path.exists(datadir):
    os.mkdir(datadir)
    print 'data dir:',datadir
    dumpMoteLabData(datfile, datadir)
  else:
    print 'data dir exists:', datadir

if __name__ == '__main__': main()

